var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        page:1,
        listRows:10,
        isEdit:1,
        classId:null
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
    },
    getData: function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            page: _this.params.page,
            listRows: _this.params.listRows,

        };
        console.log(paramsObj);
        Api.adminquestionclasslist(paramsObj, function(data){
            console.log(data);
            _this.getDataList(data.arrList);
            _this.params.total = data.totalPages;
            layui.use('laypage', function() {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'page',
                    index: 1,
                    theme: '#3498db',
                    curr: _this.params.page,
                    count: _this.params.total, //数据总数，从服务端得到
                    hash: 'curr_page',
                    layout: ['prev', 'page', 'next', 'skip'],
                    jump: function(obj, first) {
                        _this.params.page = obj.curr;
                        //obj包含了当前分页的所有参数，比如：curr当前页
                        var Params = {
                            passport: _this.params.passport,
                            page: _this.params.page,
                            listRows: _this.params.listRows,

                        };
                        Api.adminquestionclasslist(Params, function(data) {
                            _this.getDataList(data.arrList);

                        })
                    }
                });
            });
        });
    },
    getDataList: function(data) {
        var _this = this;
        if (data && data.length > 0) {
            var html = '',
                i = 0,
                len = data.length;
            for (i; i < len; i++) {
                html += '<tr>' +
                    // '<td class="text-c" width="24">'+
                    //         '<div class="goods-checkbox">'+
                    //             '<div class="check-box" classId="' + data[i].classId + '">'+
                    //                 '<input type="checkbox" name="new" classId="' + data[i].classId + '">'+
                    //             '</div>'+
                    //         '</div>'+
                    //     '</td>'+
                    '<td class="text-c">' + data[i].classId + '</td>' +
                    '<td class="text-c">' + data[i].name + '</td>'+
                    '<td class="text-c">' + data[i].classId + '</td>';
                if (data[i].isShow==1) {
                    html+='<td class="text-c"><span classId="'+data[i].classId+'" class="switch switch-on switch-new" themeColor="#3498db"></span></td>';
                }else{
                    html+='<td class="text-c"><span classId="'+data[i].classId+'" class="switch switch-off switch-new" themeColor="#3498db"></span></td>';
                };

                html+='<td class="text-c"><input type="text" class="input-text changesort" classId="' + data[i].classId + '" value="'+data[i].sort+'"></td>' +
                         '<td class="text-c">' +
                        '<p><a classId="' + data[i].classId + '"  class="handle edit">编辑</a><a classId="' + data[i].classId + '"  class="handle delete">删除</a></p>' +
                        '</td>' +
                    '</tr>';

            }
            $('#List').html(html);
            $('tfoot').css('display', 'table-footer-group');
            _this.getDataEvent();
        } else {
            var html = '<tr>' +
                '<td class="text-c c-666" colspan="6" >暂无数据</td>' +
                '</tr>';
            $('#List').html(html);
            $('tfoot').css('display', 'none');
        }
    },
    getDataEvent: function() {
        var _this = this;
        honeySwitch.init();
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('.switch-new').on('click',function(event) {
            event.preventDefault();
            var classId=$(this).attr('classId');
            var datas=null;
            if ($(this).hasClass('switch-on')) {
                datas=1;
            }else{
                datas=0;
            };
            _this.changeShow(classId,datas);
        });

        $('.edit').on('click', function(){
            $('#modal_classify').modal('show');
            $('.modal-title').text('编辑分类');
            _this.params.isEdit=0;
            _this.params.classId=$(this).attr('classId');
            _this.editContent($(this).attr('classId'));
        });
        $('.view').click(function(event) {
            Pub.setSession('classId', $(this).attr('classId'));
            layui.use('layer', function(){
                var index = layer.open({
                    type: 2,
                    title: '帮助详情',
                    content: './help-list-detail.html'
                });
                layer.full(index);
            });
        });
        $('.delete').on('click', function(event) {
            event.preventDefault();
            var classId=$(this).attr('classId');
            layui.use('layer', function(){
                layer.confirm('您确定要删除吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(index){
                    _this.deleteEvent(classId);
                    layer.close(index);
                }, function(){

                });
            });
        });
        $('.changesort').blur(function(event) {
            var classId=$(this).attr('classId');
            var sort=$(this).val();
            var Params = {
                passport: _this.params.passport,
                classId: classId,
                sort: sort
            };
            Api.adminquestionclasssortupdate(Params, function(data) {

                    console.log(data);
            })

        });

    },
    deleteEvent:function(classId){
        var _this = this;
        var Params = {
            passport: _this.params.passport,
            classId:classId
        };
        console.log(Params);
        Api.adminquestionclassdelete(Params, function(data) {
            _this.params.page=1;
            _this.getData();

        })
    },
    changeShow:function(classId,isShow){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            classId:parseInt(classId),
            isShow:isShow,
        };
        console.log(paramsObj);
        Api.adminquestionclassshowupdate(paramsObj, function(data){
            console.log(data);
        });
    },
    editContent:function(classId){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            classId:classId
        };
        Api.adminquestionclassinfo(paramsObj, function(data){
            console.log(data);
            var className=$('#classifyName').val(data.name);
            var classImage=$('#uploadfile-1').val(data.image);
            if (data.isShow==1) {
                honeySwitch.showOn("#is_show");
            }else{

                honeySwitch.showOff("#is_show");

            };
        });

    },
    uploadImg: function(){
        var _this = this;
        Api.uploadLimit('#up_form1','#file_1', 'categoryimgurl',_this.params.passport, function(response){
            if(response.code === 0){
                console.log(response);
                $('#uploadfile-1').val(response.data.imgUrl);
                layui.use('layer', function(){
                    layer.msg('上传成功', {icon: 6});
                });
            }
        });
    },
    otherEvent:function(){
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('#classify_yes').click(function(event) {
            var className=$('#classifyName').val();
            var classImage=$('#uploadfile-1').val();
            var isShow=null;
            if ($('#is_show').hasClass('switch-on')) {
                isShow=1;
            }else{
                isShow=0;
            };
            if (className=='') {
                layui.use('layer', function(){
                    layer.msg('类型名称不能为空', {icon: 5});
                });
            }else if(classImage==''){
                layui.use('layer', function(){
                    layer.msg('分类图标不能为空', {icon: 5});
                });
            }else{
                var paramsObj = {
                    passport: _this.params.passport,
                    action:'add',
                    moduleName:'user',
                    name:className,
                    image:classImage,
                    isShow:isShow,
                    sort:0
                };
                var Obj={
                    passport: _this.params.passport,
                    action:'update',
                    moduleName:'user',
                    name:className,
                    image:classImage,
                    isShow:isShow,
                    sort:0,
                    classId:_this.params.classId
                }
                if (_this.params.isEdit==1) {
                    Api.adminquestionclassaction(paramsObj, function(data){
                        //window.location.reload();
                        _this.params.page=1;
                        _this.getData();
                        $('#modal_classify').modal('hide');
                        layui.use('layer', function(){
                            layer.msg('添加成功', {icon: 6});
                        });
                    });
                }else{
                    Api.adminquestionclassaction(Obj, function(data){
                        //window.location.reload();
                        _this.params.page=1;
                        _this.getData();
                        $('#modal_classify').modal('hide');
                        layui.use('layer', function(){
                            layer.msg('编辑成功', {icon: 6});
                        });
                        _this.params.isEdit=1;
                    });
                };

            };
        });
        $('.close_classify').click(function(event) {
            $('#classifyName').val('');
            $('#uploadfile-1').val('');
            honeySwitch.showOn("#is_show");
        });

        $('#add_classify').on('click', function(){
            $('#modal_classify').modal('show');
            $('.modal-title').text('添加分类');
            $('#classifyName').val('');
            $('#uploadfile-1').val('');
            honeySwitch.showOn("#is_show");
        });
    },
    initEvent: function(){
        var _this = this;
        _this.getData();
        _this.otherEvent();
        _this.uploadImg();


    }
};
var page = new Page();